{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### numpy 数组操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "A = np.array(range(12))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2],\n",
       "       [ 3,  4,  5],\n",
       "       [ 6,  7,  8],\n",
       "       [ 9, 10, 11]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = A.reshape(4,3)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2]\n",
      " [ 3  4  5]\n",
      " [ 6  7  8]\n",
      " [ 9 10 11]]\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11]\n",
      "[ 0  3  6  9  1  4  7 10  2  5  8 11]\n"
     ]
    }
   ],
   "source": [
    "B = A.copy()\n",
    "C = B.ravel(order = 'C') # ‘C’ 按行， ‘F’ 按列\n",
    "D = B.ravel(order = 'F') # 展平一个数组\n",
    "print(B)\n",
    "print(C)\n",
    "print(D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  3  6  9]\n",
      " [ 1  4  7 10]\n",
      " [ 2  5  8 11]]\n",
      "[[ 0  3  6  9]\n",
      " [ 1  4  7 10]\n",
      " [ 2  5  8 11]]\n"
     ]
    }
   ],
   "source": [
    "# numpy.transpose(arr,axes) \n",
    "# arr：要操作的数组\n",
    "# axes：整数列表，对应维度，通常所有维度都会对换。\n",
    "C = np.transpose(A) # 翻转数组\n",
    "print(C)\n",
    "print(A.T) # 转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3,  4,  5],\n",
       "        [ 6,  7,  8,  9, 10, 11],\n",
       "        [12, 13, 14, 15, 16, 17],\n",
       "        [18, 19, 20, 21, 22, 23]],\n",
       "\n",
       "       [[24, 25, 26, 27, 28, 29],\n",
       "        [30, 31, 32, 33, 34, 35],\n",
       "        [36, 37, 38, 39, 40, 41],\n",
       "        [42, 43, 44, 45, 46, 47]],\n",
       "\n",
       "       [[48, 49, 50, 51, 52, 53],\n",
       "        [54, 55, 56, 57, 58, 59],\n",
       "        [60, 61, 62, 63, 64, 65],\n",
       "        [66, 67, 68, 69, 70, 71]]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array(range(72)).reshape(3,4,6)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\n",
      " [[[ 0 24 48]\n",
      "  [ 6 30 54]\n",
      "  [12 36 60]\n",
      "  [18 42 66]]\n",
      "\n",
      " [[ 1 25 49]\n",
      "  [ 7 31 55]\n",
      "  [13 37 61]\n",
      "  [19 43 67]]\n",
      "\n",
      " [[ 2 26 50]\n",
      "  [ 8 32 56]\n",
      "  [14 38 62]\n",
      "  [20 44 68]]\n",
      "\n",
      " [[ 3 27 51]\n",
      "  [ 9 33 57]\n",
      "  [15 39 63]\n",
      "  [21 45 69]]\n",
      "\n",
      " [[ 4 28 52]\n",
      "  [10 34 58]\n",
      "  [16 40 64]\n",
      "  [22 46 70]]\n",
      "\n",
      " [[ 5 29 53]\n",
      "  [11 35 59]\n",
      "  [17 41 65]\n",
      "  [23 47 71]]]\n",
      "\n",
      "A的转置:\n",
      " [[[ 0 24 48]\n",
      "  [ 6 30 54]\n",
      "  [12 36 60]\n",
      "  [18 42 66]]\n",
      "\n",
      " [[ 1 25 49]\n",
      "  [ 7 31 55]\n",
      "  [13 37 61]\n",
      "  [19 43 67]]\n",
      "\n",
      " [[ 2 26 50]\n",
      "  [ 8 32 56]\n",
      "  [14 38 62]\n",
      "  [20 44 68]]\n",
      "\n",
      " [[ 3 27 51]\n",
      "  [ 9 33 57]\n",
      "  [15 39 63]\n",
      "  [21 45 69]]\n",
      "\n",
      " [[ 4 28 52]\n",
      "  [10 34 58]\n",
      "  [16 40 64]\n",
      "  [22 46 70]]\n",
      "\n",
      " [[ 5 29 53]\n",
      "  [11 35 59]\n",
      "  [17 41 65]\n",
      "  [23 47 71]]]\n"
     ]
    }
   ],
   "source": [
    "C = np.transpose(A)\n",
    "print('C:\\n',C)\n",
    "print('\\nA的转置:\\n',A.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 连接数组\n",
    "# numpy.concatenate((a1, a2, ...), axis)\n",
    "# a1, a2, ...：相同类型的数组\n",
    "# axis：沿着它连接数组的轴，默认为 0\n",
    "A = np.array(range(12)).reshape(3,4)\n",
    "B = np.array(range(20,32)).reshape(3,4)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20, 21, 22, 23],\n",
       "       [24, 25, 26, 27],\n",
       "       [28, 29, 30, 31]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [20, 21, 22, 23],\n",
       "       [24, 25, 26, 27],\n",
       "       [28, 29, 30, 31]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((A,B))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3, 20, 21, 22, 23],\n",
       "       [ 4,  5,  6,  7, 24, 25, 26, 27],\n",
       "       [ 8,  9, 10, 11, 28, 29, 30, 31]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((A,B),axis = 1) # 沿着轴 1 连接两个数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]],\n",
       "\n",
       "       [[20, 21, 22, 23],\n",
       "        [24, 25, 26, 27],\n",
       "        [28, 29, 30, 31]]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy.stack 函数用于沿新轴连接数组序列，格式如下:\n",
    "# numpy.stack(arrays, axis)\n",
    "np.stack((A,B),0) # 沿轴 0 堆叠两个数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [20, 21, 22, 23]],\n",
       "\n",
       "       [[ 4,  5,  6,  7],\n",
       "        [24, 25, 26, 27]],\n",
       "\n",
       "       [[ 8,  9, 10, 11],\n",
       "        [28, 29, 30, 31]]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.stack((A,B),1) # 沿轴 1 堆叠两个数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3, 20, 21, 22, 23],\n",
       "       [ 4,  5,  6,  7, 24, 25, 26, 27],\n",
       "       [ 8,  9, 10, 11, 28, 29, 30, 31]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy.hstack 是 numpy.stack 函数的变体\n",
    "# 它通过水平堆叠来生成数组。\n",
    "np.hstack((A,B))\n",
    "# np.hstack((A,B),1) 是错误的用法，没有 1 维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [20, 21, 22, 23],\n",
       "       [24, 25, 26, 27],\n",
       "       [28, 29, 30, 31]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy.vstack 是 numpy.stack 函数的变体\n",
    "# 它通过垂直堆叠来生成数组\n",
    "np.vstack((A,B))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 分割数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy.split(ary, indices_or_sections, axis)\n",
    "# indices_or_sections：如果是一个整数，就用该数平均切分，\n",
    "# 如果是一个数组，为沿轴切分的位置（左开右闭)\n",
    "\n",
    "\n",
    "np.split(A,3) # 将数组分为三个大小相等的子数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 11 12 13 14 15 16 17 18 19 20 21]\n",
      "[array([10]), array([11, 12]), array([13, 14, 15, 16, 17, 18]), array([19, 20, 21])]\n",
      "[array([], shape=(0, 4), dtype=int64), array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]\n"
     ]
    }
   ],
   "source": [
    "C = np.array(range(10,22))\n",
    "print(C)\n",
    "print(np.split(C,[1,3,9])) # 将数组在一维数组中表明的位置分割\n",
    "print(np.split(A,[0,2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1],\n",
       "        [4, 5],\n",
       "        [8, 9]]), array([[ 2,  3],\n",
       "        [ 6,  7],\n",
       "        [10, 11]])]"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy.hsplit 函数用于水平分割数组，\n",
    "# 通过指定要返回的相同形状的数组数量来拆分原数组\n",
    "np.hsplit(A,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy.vsplit 沿着垂直轴分割数组\n",
    "np.vsplit(A, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#####  数组元素的添加与删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4)\n",
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]]\n",
      "\n",
      " [[ 6  7  8]\n",
      "  [ 9 10 11]]]\n",
      "(3, 4)\n"
     ]
    }
   ],
   "source": [
    "print(A.shape) \n",
    "B = np.resize(A,(2,2,3)) # 返回指定形状的新数组\n",
    "print(B)\n",
    "print(A.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 99 98 97 96]\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 99 98 97 96]\n"
     ]
    }
   ],
   "source": [
    "# 添加数组\n",
    "# 不表示添在哪一个维度，append 函数返回的始终是一个一维数组\n",
    "B = np.append(A,[[99,98,97,96]]) \n",
    "C = np.append(A,[99,98,97,96]) \n",
    "print(B)\n",
    "print(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [99, 98, 97, 96]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.append(A,[[99,98,97,96]],axis = 0) # 注意两层中括号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[99]\n",
      " [98]\n",
      " [97]\n",
      " [96]]\n",
      "(4, 1) (3, 4)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3, 20, 21, 22, 23],\n",
       "       [ 4,  5,  6,  7, 24, 25, 26, 27],\n",
       "       [ 8,  9, 10, 11, 28, 29, 30, 31]])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D = np.array([[99],\n",
    "     [98],\n",
    "     [97],\n",
    "     [96]])\n",
    "print(D)\n",
    "print(D.shape,A.shape)\n",
    "B = np.array([[20, 21, 22, 23],\n",
    "       [24, 25, 26, 27],\n",
    "       [28, 29, 30, 31]])\n",
    "np.append(A,B,axis = 1)\n",
    "# np.append(A,D,axis = 1) # 看来两个矩阵的列数也得一样，不然报错\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3,  4,  5],\n",
       "        [ 6,  7,  8,  9, 10, 11],\n",
       "        [12, 13, 14, 15, 16, 17],\n",
       "        [18, 19, 20, 21, 22, 23]],\n",
       "\n",
       "       [[24, 25, 26, 27, 28, 29],\n",
       "        [30, 31, 32, 33, 34, 35],\n",
       "        [36, 37, 38, 39, 40, 41],\n",
       "        [42, 43, 44, 45, 46, 47]],\n",
       "\n",
       "       [[48, 49, 50, 51, 52, 53],\n",
       "        [54, 55, 56, 57, 58, 59],\n",
       "        [60, 61, 62, 63, 64, 65],\n",
       "        [66, 67, 68, 69, 70, 71]]])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array(range(72)).reshape(3,4,6)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[  0,   1,   2,   3,   4,   5],\n",
       "        [  6,   7,   8,   9,  10,  11],\n",
       "        [ 12,  13,  14,  15,  16,  17],\n",
       "        [ 18,  19,  20,  21,  22,  23]],\n",
       "\n",
       "       [[ 24,  25,  26,  27,  28,  29],\n",
       "        [ 30,  31,  32,  33,  34,  35],\n",
       "        [ 36,  37,  38,  39,  40,  41],\n",
       "        [ 42,  43,  44,  45,  46,  47]],\n",
       "\n",
       "       [[666, 666, 666, 666, 666, 666],\n",
       "        [666, 666, 666, 666, 666, 666],\n",
       "        [666, 666, 666, 666, 666, 666],\n",
       "        [666, 666, 666, 666, 666, 666]],\n",
       "\n",
       "       [[ 48,  49,  50,  51,  52,  53],\n",
       "        [ 54,  55,  56,  57,  58,  59],\n",
       "        [ 60,  61,  62,  63,  64,  65],\n",
       "        [ 66,  67,  68,  69,  70,  71]]])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 必须提供轴参数，不然数组会被展开成一维\n",
    "np.insert(A,2,[666],axis=0) # 沿 0 轴在 2 位置 插入[666]，维度不足，广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[  0,   1,   2,   3,   4,   5],\n",
       "        [  6,   7,   8,   9,  10,  11],\n",
       "        [666, 666, 666, 666, 666, 666],\n",
       "        [ 12,  13,  14,  15,  16,  17],\n",
       "        [ 18,  19,  20,  21,  22,  23]],\n",
       "\n",
       "       [[ 24,  25,  26,  27,  28,  29],\n",
       "        [ 30,  31,  32,  33,  34,  35],\n",
       "        [666, 666, 666, 666, 666, 666],\n",
       "        [ 36,  37,  38,  39,  40,  41],\n",
       "        [ 42,  43,  44,  45,  46,  47]],\n",
       "\n",
       "       [[ 48,  49,  50,  51,  52,  53],\n",
       "        [ 54,  55,  56,  57,  58,  59],\n",
       "        [666, 666, 666, 666, 666, 666],\n",
       "        [ 60,  61,  62,  63,  64,  65],\n",
       "        [ 66,  67,  68,  69,  70,  71]]])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.insert(A,2,[666],axis=1) # 沿 1 轴在 2 位置 插入[666]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[  0,   1, 666,   2,   3,   4,   5],\n",
       "        [  6,   7, 666,   8,   9,  10,  11],\n",
       "        [ 12,  13, 666,  14,  15,  16,  17],\n",
       "        [ 18,  19, 666,  20,  21,  22,  23]],\n",
       "\n",
       "       [[ 24,  25, 666,  26,  27,  28,  29],\n",
       "        [ 30,  31, 666,  32,  33,  34,  35],\n",
       "        [ 36,  37, 666,  38,  39,  40,  41],\n",
       "        [ 42,  43, 666,  44,  45,  46,  47]],\n",
       "\n",
       "       [[ 48,  49, 666,  50,  51,  52,  53],\n",
       "        [ 54,  55, 666,  56,  57,  58,  59],\n",
       "        [ 60,  61, 666,  62,  63,  64,  65],\n",
       "        [ 66,  67, 666,  68,  69,  70,  71]]])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.insert(A,2,[666],axis=2) # 沿 2 轴在 2 位置 插入[666]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 6)\n",
      "(3, 4, 6)\n",
      "# 沿 1 轴在 2 位置 插入 I\n",
      "[[[  0   1   2   3   4   5]\n",
      "  [  6   7   8   9  10  11]\n",
      "  [666 777 888 999 555 444]\n",
      "  [ 12  13  14  15  16  17]\n",
      "  [ 18  19  20  21  22  23]]\n",
      "\n",
      " [[ 24  25  26  27  28  29]\n",
      "  [ 30  31  32  33  34  35]\n",
      "  [999 888 777 666 555 444]\n",
      "  [ 36  37  38  39  40  41]\n",
      "  [ 42  43  44  45  46  47]]\n",
      "\n",
      " [[ 48  49  50  51  52  53]\n",
      "  [ 54  55  56  57  58  59]\n",
      "  [111 222 333 444 555 666]\n",
      "  [ 60  61  62  63  64  65]\n",
      "  [ 66  67  68  69  70  71]]]\n",
      "# 沿 1 轴在 2 位置 插入 I2\n",
      "[[[  0   1   2   3   4   5]\n",
      "  [  6   7   8   9  10  11]\n",
      "  [111 222 333 444 555 666]\n",
      "  [ 12  13  14  15  16  17]\n",
      "  [ 18  19  20  21  22  23]]\n",
      "\n",
      " [[ 24  25  26  27  28  29]\n",
      "  [ 30  31  32  33  34  35]\n",
      "  [111 222 333 444 555 666]\n",
      "  [ 36  37  38  39  40  41]\n",
      "  [ 42  43  44  45  46  47]]\n",
      "\n",
      " [[ 48  49  50  51  52  53]\n",
      "  [ 54  55  56  57  58  59]\n",
      "  [111 222 333 444 555 666]\n",
      "  [ 60  61  62  63  64  65]\n",
      "  [ 66  67  68  69  70  71]]]\n"
     ]
    }
   ],
   "source": [
    "I = np.array([[666,777,888,999,555,444],\n",
    "    [999,888,777,666,555,444],\n",
    "    [111,222,333,444,555,666]])\n",
    "I2 = [111,222,333,444,555,666]\n",
    "print(I.shape)\n",
    "print(A.shape)\n",
    "print('# 沿 1 轴在 2 位置 插入 I')\n",
    "print(np.insert(A,2,I,axis=1)) # 沿 1 轴在 2 位置 插入 I\n",
    "print('# 沿 1 轴在 2 位置 插入 I2')\n",
    "print(np.insert(A,2,I2,axis=1)) # 沿 1 轴在 2 位置 插入 I2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3,  4,  5],\n",
       "        [ 6,  7,  8,  9, 10, 11],\n",
       "        [12, 13, 14, 15, 16, 17],\n",
       "        [18, 19, 20, 21, 22, 23]],\n",
       "\n",
       "       [[24, 25, 26, 27, 28, 29],\n",
       "        [30, 31, 32, 33, 34, 35],\n",
       "        [36, 37, 38, 39, 40, 41],\n",
       "        [42, 43, 44, 45, 46, 47]],\n",
       "\n",
       "       [[48, 49, 50, 51, 52, 53],\n",
       "        [54, 55, 56, 57, 58, 59],\n",
       "        [60, 61, 62, 63, 64, 65],\n",
       "        [66, 67, 68, 69, 70, 71]]])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3,  4,  5],\n",
       "        [ 6,  7,  8,  9, 10, 11],\n",
       "        [18, 19, 20, 21, 22, 23]],\n",
       "\n",
       "       [[24, 25, 26, 27, 28, 29],\n",
       "        [30, 31, 32, 33, 34, 35],\n",
       "        [42, 43, 44, 45, 46, 47]],\n",
       "\n",
       "       [[48, 49, 50, 51, 52, 53],\n",
       "        [54, 55, 56, 57, 58, 59],\n",
       "        [66, 67, 68, 69, 70, 71]]])"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.delete(A,2,axis = 1) # 删除 1 轴 位置 2 的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
